var t = require("../../0E8B0BC42FB173BF68ED63C34BB4DCA6.js"), e = require("../../30A2BE152FB173BF56C4D61233F4DCA6.js"), s = require("../../A7DBB9B62FB173BFC1BDD1B19505DCA6.js"), i = s.getSysInfo(), n = i.windowWidth, a = i.windowHeight, o = [ "#EE00EE", "#00EE00", "#EEEE00", "#00E5EE", "#EE7942" ], c = .1 * Math.PI, r = 15 / 360 * 2 * Math.PI, h = 360 / 28 / 360 * 2 * Math.PI, l = wx.createInnerAudioContext(), u = wx.createInnerAudioContext(), d = wx.createInnerAudioContext(), x = wx.createInnerAudioContext(), w = wx.createInnerAudioContext(), v = getApp().globalData.settings;

Page({
    canvasContext: null,
    users: [],
    radius: {},
    timer: null,
    startSelect: !1,
    targetIdx: -1,
    selectTimer: null,
    selectInterval: null,
    selectTimes: -1,
    selectBgElastic: 0,
    winBgElastic: 0,
    winBgDir: 1,
    winInfo: null,
    random: 0,
    countdownSec: 3,
    countdownTxt: "",
    countdownTimer: null,
    countdown: !1,
    selectResult: !1,
    selectBtnRect: null,
    selectImageName: "btn",
    autoPunishmentChecked: v.autoPunishmentChecked,
    autoStart: v.autoStart,
    data: {
        isIpx: getApp().globalData.isIpx,
        windowWidth: n,
        windowHeight: a
    },
    onLoad: function() {
        wx && wx.createCanvasContext || getApp().versionLow(), l.src = "/mp3/press.mp3", 
        u.src = "/mp3/exit.mp3", d.src = "/mp3/select.mp3", x.src = "/mp3/win.mp3", w.src = "/mp3/countdown.mp3";
    },
    onShow: function() {
        this.init(), wx && wx.createCanvasContext ? (this.canvasContext = wx.createCanvasContext("fingerCanvas"), 
        this.autoStart ? (this.drawText("请放好手指准备"), this.canvasContext.draw()) : this.drawSelectBtn()) : getApp().versionLow();
    },
    init: function() {
        this.startSelect = !1, this.selectResult = !0, this.selectTimes = -1, this.winInfo = null;
    },
    startAnimation: function() {
        this.timer || (this.drawUsers(), this.timer = setInterval(this.drawUsers.bind(this), 50));
    },
    onHide: function() {
        this.timer && (clearInterval(this.timer), this.timer = null), this.clearCountDown(), 
        this.users.length = 0, this.canvasContext && (this.canvasContext.clearRect(0, 0, n, a), 
        this.canvasContext.draw(), this.canvasContext = null);
    },
    isTouchSelect: function(t) {
        var e = t.x, s = t.y;
        if (!this.autoStart && this.selectBtnRect) {
            var i = this.selectBtnRect, n = i.xStart, a = i.xEnd, o = i.yStart, c = i.yEnd;
            if (e >= n && e <= a && s >= o && s <= c) return !0;
        }
        return !1;
    },
    onTouchStart: function(t) {
        if (!this.countdown) {
            var e = this, i = t.touches;
            if (this.isTouchSelect(i[i.length - 1])) return this.onStartSelect();
            this.autoStart || (this.selectImageName = "btn_press", this.drawSelectBtn(), setTimeout(function() {
                e.selectImageName = "btn", e.drawSelectBtn();
            }, 300));
            var n = [].concat(i.filter(function(t) {
                return !e.isTouchSelect(t);
            }));
            this.users.length !== n.length && (this.targetIdx = -1), this.users = n, this.startAnimation(), 
            s.vibrate(), l.play(), this.autoStart ? (this.clearCountDown(), s.isIOS() && this.users.length >= 5 ? (this.countdownTxt = "", 
            this.countdownSec = 3, this.onStartSelect()) : this.users.length > 1 && (this.countdownSec = 3, 
            this.startCountDown(1e3))) : s.isIOS() && this.users.length >= 5 && this.onStartSelect();
        }
    },
    onTouchEnd: function(t) {
        if (!this.countdown) {
            var e = t.changedTouches[0], s = !1;
            if (e) {
                if (this.isTouchSelect(e)) return;
                var i = this.users.findIndex(function(t) {
                    return t.identifier === e.identifier;
                });
                -1 !== i && (this.countdown || (delete this.radius[this.users[i].identifier], this.users.splice(i, 1), 
                u.play(), s = !0));
            }
            s && !this.selectResult && (this.clearSelect(), this.autoStart && (this.users.length ? (this.clearCountDown(), 
            this.countdownSec = 3, this.countdownTxt = "", this.startCountDown(1e3)) : (this.clearCountDown(), 
            this.countdownTxt = "请放好手指准备2")));
        }
    },
    onTouchMove: function(t) {
        if (!this.countdown) {
            var e = this, s = t.touches;
            this.users = [].concat(s.filter(function(t) {
                return !e.isTouchSelect(t);
            }));
        }
    },
    startCountDown: function(t) {
        var e = this;
        this.countdownTimer = setTimeout(function() {
            e.countdownTxt = "倒计时" + e.countdownSec-- + "秒", e.countdownSec < 0 ? (e.countdownTxt = "", 
            e.countdownSec = 3, e.clearCountDown(), e.onStartSelect()) : (e.startCountDown(1e3), 
            e.countdown || (e.countdown = !0, w.play()));
        }, t);
    },
    clearCountDown: function() {
        this.countdownTimer && (clearTimeout(this.countdownTimer), this.countdownTimer = null, 
        this.selectResult = !1, w.stop());
    },
    drawUsers: function() {
        this.canvasContext && (this.canvasContext.clearRect(0, 0, n, a), this.users.forEach(this.drawUser.bind(this)), 
        this.drawWinInfo(), this.autoStart ? this.countdownTxt && this.drawText(this.countdownTxt) : this.drawSelectBtn(!1), 
        this.canvasContext.draw());
    },
    drawWinInfo: function() {
        this.winInfo && (this.winBgDir > 0 ? (this.winBgElastic += 8, this.winBgElastic + 200 >= 270 && (this.winBgDir = -1)) : (this.winBgElastic -= 8, 
        this.winBgElastic <= 0 && (this.winBgDir = 1)), this.drawSelectImg(this.winInfo, 200, this.winBgElastic, "win"));
    },
    drawText: function(t) {
        if (this.canvasContext) {
            this.canvasContext.beginPath();
            var e = n / 2, s = a / 2;
            this.canvasContext.fillStyle = "white", this.canvasContext.font = "28px Arial";
            var i = this.canvasContext.measureText(t);
            this.canvasContext.translate(e - 12, s - i.width / 2), this.canvasContext.rotate(90 * Math.PI / 180), 
            this.canvasContext.fillText(t, 0, 0), this.canvasContext.closePath();
        }
    },
    drawSelectBtn: function() {
        var t = !(arguments.length > 0 && void 0 !== arguments[0]) || arguments[0];
        if (this.canvasContext) {
            this.canvasContext.beginPath();
            var e = (n - 63.6) / 2, s = (a - 200.4) / 2;
            this.selectBtnRect = {
                xStart: e,
                xEnd: e + 63.6,
                yStart: s,
                yEnd: s + 200.4
            }, this.canvasContext.drawImage("/assets/" + this.selectImageName + ".png", e, s, 63.6, 200.4), 
            this.canvasContext.closePath(), t && this.canvasContext.draw();
        }
    },
    drawUser: function(t, e) {
        var s = t.x, i = t.y, n = t.identifier, a = this.radius[n];
        a ? (a.dir > 0 ? (a.firstElastic += .08, a.secondElastic += .08, a.thirdElastic += .08, 
        this.selectBgElastic += 1, a.radius += 1) : a.dir < 0 && (a.firstElastic -= .08, 
        a.secondElastic -= .08, a.thirdElastic -= .08, this.selectBgElastic -= 1, a.radius -= 1), 
        a.firstRotate += .05, a.secondRotate -= .05, a.thirdRotate += .05, a.radius > 60 ? (a.dir = -1, 
        a.radius = 60) : a.radius < 50 && (a.dir = 1, a.radius = 50)) : this.radius[n] = a = {
            radius: 50,
            dir: 1,
            firstElastic: 0,
            secondElastic: 0,
            thirdElastic: 0,
            firstRotate: 0,
            secondRotate: 0,
            thirdRotate: 0
        };
        var l = o[e % o.length], u = !this.startSelect && -1 !== this.targetIdx;
        u && this.targetIdx !== e && (l = "gray"), this.canvasContext.beginPath(), this.canvasContext.arc(s, i, a.radius, 0, 2 * Math.PI), 
        this.canvasContext.fillStyle = l, this.canvasContext.fill(), this.canvasContext.closePath();
        for (var d = 0; d < 20; d++) {
            var x = s + Math.cos(c * d + a.firstRotate) * (a.radius + 10 * (1 + a.firstElastic)), w = i + Math.sin(c * d + a.firstRotate) * (a.radius + 10 * (1 + a.firstElastic));
            this.canvasContext.moveTo(x, w), this.canvasContext.beginPath(), this.canvasContext.arc(x, w, 6, 0, 2 * Math.PI), 
            this.canvasContext.fillStyle = l, this.canvasContext.fill(), this.canvasContext.closePath();
        }
        for (var v = 0; v < 24; v++) x = s + Math.cos(r * v + a.secondRotate) * (a.radius + 20 * (1 + a.secondElastic)), 
        w = i + Math.sin(r * v + a.secondRotate) * (a.radius + 20 * (1 + a.secondElastic)), 
        this.canvasContext.moveTo(x, w), this.canvasContext.beginPath(), this.canvasContext.arc(x, w, 4, 0, 2 * Math.PI), 
        this.canvasContext.fillStyle = l, this.canvasContext.fill(), this.canvasContext.closePath();
        for (var f = 0; f < 28; f++) x = s + Math.cos(h * f + a.thirdRotate) * (a.radius + 30 * (1 + a.thirdElastic)), 
        w = i + Math.sin(h * f + a.thirdRotate) * (a.radius + 30 * (1 + a.thirdElastic)), 
        this.canvasContext.moveTo(x, w), this.canvasContext.beginPath(), this.canvasContext.arc(x, w, 3, 0, 2 * Math.PI), 
        this.canvasContext.fillStyle = l, this.canvasContext.fill(), this.canvasContext.closePath();
        this.startSelect && this.targetIdx === e ? this.drawSelectImg({
            x: s,
            y: i,
            radius: a.radius
        }, 110, this.selectBgElastic, "select") : u && this.targetIdx === e && !this.winInfo && (this.winInfo = {
            x: s,
            y: i,
            radius: 50
        });
    },
    drawSelectImg: function(t, e, s, i) {
        var n = t.radius, a = t.x, o = t.y;
        this.canvasContext.beginPath();
        var c = n + e + s;
        this.canvasContext.drawImage("/assets/" + i + ".png", a - c, o - c, 2 * c, 2 * c), 
        this.canvasContext.closePath();
    },
    onStartSelect: function() {
        var t = this.users.length;
        console.log("onStartSelect:", t), 0 === t ? wx.showToast({
            title: "请先把手指放置到屏幕上",
            icon: "none"
        }) : t > 1 && (this.startSelect = !0, this.random = s.randomNum(0, t), this.clearSelect(), 
        this.onSelect());
    },
    clearSelect: function() {
        this.selectTimer && (clearTimeout(this.selectTimer), this.selectTimer = null), this.selectInterval = 0, 
        this.selectTimes = -1, this.targetIdx = -1, this.winBgElastic = 0, this.selectBgElastic = 0;
    },
    onSelect: function() {
        var t = this;
        this.selectTimer = setTimeout(function() {
            t.selectTimes++, t.targetIdx++, t.targetIdx >= t.users.length && (t.targetIdx = 0), 
            t.selectTimes <= 16 + t.random ? (d.play(), t.selectTimes > 10 + t.random ? t.selectInterval = 100 + 40 * (6 - (16 - t.selectTimes)) : t.selectInterval = 100, 
            t.onSelect()) : (x.play(), s.vibrate(), t.punishment());
        }, this.selectInterval);
    },
    punishment: function() {
        var t = this;
        setTimeout(function() {
            t.countdown = !1, t.autoPunishmentChecked && wx.navigateTo({
                url: "/pages/punishment/index?mode=picker"
            });
        }, 3e3);
    },
    onShareAppMessage: function(s) {
        return {
            title: e(t.SHARE.title)[0],
            path: "/pages/index/index",
            imageUrl: "/assets/" + e(t.SHARE.image)[0]
        };
    },
    onHelp: function() {
        wx.navigateTo({
            url: "/page/help/help"
        });
    },
    onBack: function() {
        s.navigateBack();
    }
});
// 路羽资源博客 https://www.luyuz.cn/